package com.computer.network.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.computer.network.mapper.SysUserMapper;
import com.computer.network.mapper.SysUserRoleRelationMapper;
import com.computer.network.po.SysUser;
import com.computer.network.po.SysUserRoleRelation;
import com.computer.network.security.ex.RedisRunException;
import com.computer.network.service.AuthorityService;
import com.computer.network.service.SysUserService;
import com.computer.network.util.MyHashMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysUserServiceImpl implements SysUserService {
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SysUserRoleRelationMapper sysUserRoleRelationMapper;
    @Resource
    private AuthorityService authorityService;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public SysUser findUserByUserName(String userName) {
        QueryWrapper<SysUser> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("user_name", userName);
        SysUser sysUser = sysUserMapper.selectOne(userQueryWrapper);
        return sysUser;
    }

    @Override
    public List<String> selectUrlListByUserId(Long userId) {
        SysUser sysUser = new SysUser();
        sysUser.setId(userId);
        List<String> stringList = authorityService.selectUser(sysUser);
        return stringList;
    }

    @Override
    public List<Long> selectRoleNameByUserId(Long userId) {
        QueryWrapper<SysUserRoleRelation> queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId);
        List<SysUserRoleRelation> sysUserRoleRelations = sysUserRoleRelationMapper.selectList(queryWrapper);
        List<Long> collect = sysUserRoleRelations.stream().map(SysUserRoleRelation::getRoleId).collect(Collectors.toList());
        return collect;
    }

    @Override
    public String checkRole(String url) {
        authorityService.redisTable("authority1.1");
        //查询动态权限表
        Object o = redisTemplate.opsForValue().get("authority1.1");
        System.out.println(o);
        HashMap authorityHashMap = (HashMap)redisTemplate.opsForValue().get("authority1.1");
        if (authorityHashMap == null) {
            throw new RedisRunException("未加载权限表");
        }
        //查询当前权限属于哪个角色id
        String roleId = (String) authorityHashMap.get(url);
        if (roleId != null) {
            return roleId;
        }
        //权限表内可能为/xx/*/*或/xx/*/xx格式
        StringBuffer stringBuffer1 = new StringBuffer(url);
        //去除第一个/
        StringBuffer stringBuffer2 = new StringBuffer(stringBuffer1.substring(1));
        int i = stringBuffer2.indexOf("/");
        //第一个地址a
        StringBuffer a = new StringBuffer(stringBuffer2.substring(0, i));
        // /xx/*/*
        String url1 = "/" + a + "/*/*";
        String roleId2 = (String) authorityHashMap.get(url1);
        if (roleId2 != null) {
            return roleId2;
        }
        //第二个地址c
        StringBuffer b = new StringBuffer(stringBuffer2.substring(i + 1));
        int j = b.indexOf("/");
        StringBuffer c = new StringBuffer(b.substring(0, j));
        // /xx/xx/*
        String url2 = "/" + a + "/" + c + "/*";
        String roleId3 = (String) authorityHashMap.get(url2);
        if (roleId3 != null) {
            return roleId3;
        }
        return null;
    }
}
